package sun.duandianxiazai;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ProgressBar;

import com.example.johnsun.mytestapp.R;

public class DownloadListActivity extends Activity {
private ProgressBar progressBar;
    private MyReceiver mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_list);
        progressBar= (ProgressBar) findViewById(R.id.progressBar);
        mReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.yyxu.download.activities.DownloadListActivity");
        registerReceiver(mReceiver, filter);




    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();



    }

    public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            handleIntent(intent);

        }

        private void handleIntent(Intent intent) {

            if (intent != null
                    && intent.getAction().equals(
                    "com.yyxu.download.activities.DownloadListActivity")) {
                int type = intent.getIntExtra(MyIntents.TYPE, -1);
                String url;

                switch (type) {
                    case MyIntents.Types.ADD:
                        url = intent.getStringExtra(MyIntents.URL);
                        boolean isPaused = intent.getBooleanExtra(MyIntents.IS_PAUSED, false);
//                        if (!TextUtils.isEmpty(url)) {
//                            downloadListAdapter.addItem(url, isPaused);
//                        }
                        break;
                    case MyIntents.Types.COMPLETE:
                        url = intent.getStringExtra(MyIntents.URL);

//                        if (!TextUtils.isEmpty(url)) {
//                            downloadListAdapter.removeItem(url);
//                        }
                        break;
                    case MyIntents.Types.PROCESS:
                     String progress =  intent.getStringExtra(MyIntents.PROCESS_PROGRESS);
//                        url = intent.getStringExtra(MyIntents.URL);
//                        View taskListItem = downloadList.findViewWithTag(url);
//                        ViewHolder viewHolder = new ViewHolder(taskListItem);
//                        viewHolder.setData(url, intent.getStringExtra(MyIntents.PROCESS_SPEED),
//                                intent.getStringExtra(MyIntents.PROCESS_PROGRESS));
//                        int a=Integer.valueOf(progress).intValue();
//                        progressBar.setProgress(a);
                        break;
                    case MyIntents.Types.ERROR:
                        url = intent.getStringExtra(MyIntents.URL);
                        // int errorCode =
                        // intent.getIntExtra(MyIntents.ERROR_CODE,
                        // DownloadTask.ERROR_UNKONW);
                        // handleError(url, errorCode);
                        break;
                    default:
                        break;
                }
            }
        }

    }
}
